package algorithm.leetcode.simple;

/*
 给定一个二叉树，返回所有从根节点到叶子节点的路径。
 */

import java.util.ArrayList;
import java.util.List;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/10/26
 */
public class Question257 {

    public static void main(String[] args) {

    }

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> pathList = new ArrayList<>(10);
        treePath(root, "", pathList);
        return pathList;
    }

    private void treePath(TreeNode node, String path, List<String> pathList) {
        if (node == null) {
            return;
        }
        StringBuilder sb = new StringBuilder(path);
        sb.append(node.val);
        if (node.left == null && node.right == null) {
            pathList.add(sb.toString());
            return;
        }
        sb.append("->");
        treePath(node.left, sb.toString(), pathList);
        treePath(node.right, sb.toString(), pathList);
    }

    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
